package com.wxblockchain.core.manager;

import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Set;

/*
   封装Key-Value的存储，内部存储对业务侧透明
 */
public class KVCacheManager {
    private static MMKV mmkv;

    static {
        mmkv = MMKV.defaultMMKV();
    }

    private KVCacheManager() {}

    private static class SingleHolder {
        private static final KVCacheManager SINGLE = new KVCacheManager();
    }

    public static KVCacheManager getInstance() {
        return SingleHolder.SINGLE;
    }

    public static void saveBool(String key, boolean value) {
        mmkv.encode(key,value);
    }

    public void saveInt(String key, int value) {
        mmkv.encode(key,value);
    }

    public void saveLong(String key, long value) {
        mmkv.encode(key,value);
    }

    public void saveFloat(String key, float value) {
        mmkv.encode(key,value);
    }

    public void saveDouble(String key, double value) {
        mmkv.encode(key,value);
    }

    public void saveString(String key, String value) {
        mmkv.encode(key,value);
    }

    public void saveBytes(String key, byte[] value) {
        mmkv.encode(key,value);
    }

    public void saveParcelabled(String key, Parcelable value) {
        mmkv.encode(key,value);
    }

    public void saveSets(String key, Set<String> value) {
        mmkv.encode(key,value);
    }

    public boolean getBool(String key) {
        return mmkv.decodeBool(key, false);
    }

    public boolean getBool(String key, boolean defaultvalue) {
        return mmkv.decodeBool(key, defaultvalue);
    }

    public int getInt(String key) {
        return mmkv.decodeInt(key);
    }

    public int getInt(String key, int defaultvalue) {
        return mmkv.decodeInt(key,defaultvalue);
    }

    public float getFloat(String key) {
        return mmkv.decodeFloat(key);
    }

    public float getFloat(String key, float defaultvalue) {
        return mmkv.decodeFloat(key,defaultvalue);
    }

    public double getDouble(String key) {
        return mmkv.decodeDouble(key);
    }

    public double getDouble(String key, double defaultvalue) {
        return mmkv.decodeDouble(key,defaultvalue);
    }

    public String getString(String key) {
        return mmkv.decodeString(key);
    }

    public String getString(String key, String defaultvalue) {
        return mmkv.decodeString(key,defaultvalue);
    }

    public byte[] getBytes(String key) {
        return mmkv.decodeBytes(key);
    }

    public <T extends Parcelable> T getParcelabled(String key, Class<T> tClass) {
        return mmkv.decodeParcelable(key,tClass);
    }

    public Set<String> getSets(String key) {
        return mmkv.decodeStringSet(key);
    }
}
